package com.controller;

import com.pojo.Contact;
import com.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/contact")
public class ContactController {
    @Autowired
    private ContactService contactService;

    /**
     * 分页查询所有联系人
     * @param model
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "/list")
    public String List(Model model,@RequestParam(defaultValue = "1") int pageNo,@RequestParam(required = false) String cname){
        int pageSize = 3;
        int pageIndex = (pageNo - 1) * pageSize;
        int totalCount = contactService.count(cname); //总记录数
        int totalPageCount = totalCount/pageSize; //总页数
        if (totalCount % 3 > 0){
            totalPageCount ++;
        }
        if (pageIndex>totalPageCount){
            pageIndex=totalPageCount;
        }
        List<Contact> list = contactService.list(pageIndex,pageSize,cname);
        model.addAttribute("list",list);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("pageNo",pageNo);
        return "index";
    }

    /**
     * 按照联系人姓名模糊查询
     * @param model
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "/souList")
    public String SouList(Model model,@RequestParam(defaultValue = "1") int pageNo,@RequestParam(required = false) String cname){
        int pageSize = 3;
        int pageIndex = (pageNo - 1) * pageSize;
        int totalCount = contactService.count(cname); //总记录数
        int totalPageCount = totalCount/pageSize; //总页数
        if (totalCount == 0){
            model.addAttribute("error","没有找到相关联系人");
        }
        if (totalCount % 3 > 0){
            totalPageCount ++;
        }
        if (pageIndex>totalPageCount){
            pageIndex=totalPageCount;
        }
        List<Contact> list = contactService.list(pageIndex,pageSize,cname);
        model.addAttribute("list",list);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("pageNo",pageNo);
        return "index";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "add";
    }

    /**
     * 添加联系人
     * @param contact
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(Contact contact){
        int result = contactService.add(contact);
        System.out.println(result);
        return "redirect:/contact/list";
    }

    /**
     * 根据id查询联系人详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectId")
    public String selectId(Model model,@RequestParam int id){
        Contact contact = contactService.selectId(id);
        model.addAttribute("contact",contact);
        return "update";
    }

    /**
     * 根据id修改联系人信息
     * @param contact
     * @param id
     * @return
     */
    @RequestMapping(value = "/update")
    public String update(Contact contact,@RequestParam int id){
        int result = contactService.update(contact);
        if (result > 0){
            return "redirect:/contact/list";
        }
        return "update";
    }
}
